package jku.mobcomp;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

import java.util.Iterator;
import java.util.List;

import jku.mobcomp.helper.Const;
import jku.mobcomp.helper.Utility;

/**
 * This class handles all the CRUD operations related to
 * Country entity.
 *
 */
public class Country {

  /**
   * Update the country
   * @param name: name of the country
   * @param description : description
   * @return  updated country
   */
  public static void createOrUpdateCountry(String keyId, String name, String description) {
    Long keyIdLng = Utility.convertStringToLong(keyId);
	Entity country = getSingleCountryById(keyIdLng);
  	if (country == null) {
  	  country = new Entity(Const.ENTITY_Country); // key id is created automatically (Long) - for country also the name as Key would be possible
  	  country.setProperty(Const.PROP_name, name);
  	  country.setProperty(Const.PROP_description, description);
  	} else {        
	    if (name != null && !"".equals(name)) {
	    	country.setProperty(Const.PROP_name, name);
	    }  
	    if (description != null && !"".equals(description)) {
	    	country.setProperty(Const.PROP_description, description);
	    }     
  	}
  	Utility.persistEntity(country);
  }

  /**
   * Retrun all the countrys
   * @param kind : of kind country
   * @return  countrys
   */
  public static Iterable<Entity> getAllCountrys(String kind) {
    return Utility.listEntities(kind, null, null);
  }

  /**
   * Get country entity
   * @param name : name of the country
   * @return: country entity
   */
  public static Iterable<Entity> getCountryByName(String countryName) {
	Iterable<Entity> entities = Utility.listEntities(Const.ENTITY_Country, Const.PROP_name, countryName);
  	return entities;
  }
  
  /**
   * get Country with keyId
   * @param keyId: (Long)
   * @return  country entity
   */
  public static Entity getSingleCountryById(Long keyId) {
	  if (keyId!=null) {
		  Key keySearch = KeyFactory.createKey(Const.ENTITY_Country, keyId);
		  return Utility.findEntity(keySearch); 
	  }
	  return null;
  }

  
  /**
   * Delete country entity
   * @param countryKey: country to be deleted
   * @return status string
   */
  public static String deleteCountry(String countryKeyId)
  {
	  Long keyIdLng = Utility.convertStringToLong(countryKeyId);
	  Entity countryEntity = getSingleCountryById(keyIdLng); 
	  Iterable<Entity> boulders = Boulder.getBouldersByCountryId( countryKeyId);	  	  
	  Iterator<Entity> it = boulders.iterator();
	  if (it.hasNext()){
	      return "Cannot delete, as there are Boulders associated with this Country.";	      
	    }	    
	  Utility.deleteEntity(countryEntity.getKey());
	  return "Country " + countryKeyId + " deleted successfully";
	  
  }
}
